<?php

namespace App;

use App\View;

class View extends Base
{
    protected $_vars = array();



    /*
        if ($cache = $this->view->cache('blog.widget.post')){
            $this->view->assign($data_entry);
            $this->view->showCache('blog.widget.post');
        }
    */
    public function cache($name, $expire = 60)
    {
        ob_start();
        $file = $this->findTpl($name);
        $cache_file  = $this->findCacheFile(dirname($file) . DS, md5($file));
        $modify_time = filemtime($cache_file);

        if ($modify_time + 60 > REQUEST_TIME) {
            //直接读取缓存
            //include $cache_file;
            echo file_get_contents($cache_file);

            return ob_get_clean();
        } else {
            //生成缓存
            return FALSE;
        }
    }

    public function action($action, $controller = '')
    {

    }

    public function assign($name, $value = NULL)
    {
        if (NULL === $value && is_array($name))
        {
            $this->_vars += $name;
        }
        elseif (NULL !== $value && is_scalar($name))
        {
            $this->_vars[$name] = $value;
        }
    }

    public function fetch($tplfile)
    {
        extract($this->_vars, EXTR_OVERWRITE);
        ob_start();
        include $tplfile;

        return ob_get_clean();
    }

    public function display($tplfile)
    {
        echo $this->fetch($tplfile);
    }




}